programming4us
           
 
 
Windows Phone

Programming Windows Phone 7 : An XNA Program for the Phone (part 3)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/27/2010 11:34:29 AM

In the Draw method you want to draw on the display. But that’s all you want to do. If you need to perform some calculations in preparation for drawing, you should do those in the Update method. The Update method prepares the program for the Draw method. Very often an XNA program will be moving sprites around the display based on user input. For the phone, this user input mostly involves fingers touching the screen. All handling of user input should also occur during the Update method. 

You should write your Update and Draw methods so that they execute as quickly as possible. That’s rather obvious, I guess, but here’s something very important that might not be so obvious:

You should avoid code in Update and Draw that routinely allocates memory from the program’s local heap. Eventually the .NET garbage collector will want to reclaim some of this memory, and while the garbage collector is doing its job, your game might stutter a bit. Throughout the chapters on XNA programming, you’ll see techniques to avoid allocating memory from the heap.

Your Draw methods probably won’t contain any questionable code; it’s usually in the Update method where trouble lurks. Avoid any new expressions involving classes. These always cause memory allocation. Instantiating a structure is fine, however, because structure instances are stored on the stack and not in the heap. (XNA uses structures rather than classes for many types of objects you’ll often need to create in Update.) But heap allocations can also occur without explicit new expressions. For example, concatenating two strings creates another string on the heap. If you need to perform string manipulation in Update, you should use StringBuilder. Conveniently, XNA provides methods to display text using StringBuilder objects.

In XnaHelloPhone, however, the Update method is trivial. The text displayed by the program is anchored in one spot. All the necessary calculations have already been performed in the LoadContent method. For that reason, the Update method will be left simply as XNA Game Studio originally created it:

Example 6. XNA Project: XnaHelloPhone File: Game1.cs (excerpt)
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

base.Update(gameTime);
}


The default code uses the static GamePad class to check if the phone’s hardware Back button has been pressed and uses that to exit the game.

Finally, there is the Draw method. The version created for you simply colors the background with a light blue:

Example 7. XNA Project: XnaHelloPhone File: Game1.cs (excerpt)
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

base.Draw(gameTime);
}

The color known as CornflowerBlue has achieved iconic status in the XNA programming community. When you’re developing an XNA program, the appearance of the light blue screen is very comforting because it means the program has at least gotten as far as Draw. But if you want to conserve power on OLED displays, you want to go with darker backgrounds. In my revised version, I’ve compromised by setting the background to a darker blue. As in Silverlight, XNA supports the 140 colors that have come to be regarded as standard. The text is colored white:

Example 8. XNA Project: XnaHelloPhone File: Game1.cs (excerpt)
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Navy);

spriteBatch.Begin();
spriteBatch.DrawString(segoe14, text, textPosition, Color.White);
spriteBatch.End();

base.Draw(gameTime);
}

Sprites get out on the display by being bundled into a SpriteBatch object, which was created during the call to LoadContent. Between calls to Begin and End there can be multiple calls to DrawString to draw text and Draw to draw bitmaps. Those are the only options. This particular DrawString call references the font, the text to display, the position of the upper-left corner of the text relative to the upper-left corner of the screen, and the color. And here it is:



Oh, that’s interesting! By default, Silverlight programs come up in portrait mode, but XNA programs come up in landscape mode. Let’s turn the phone or emulator sideways:


Other -----------------
- Programming Windows Phone 7 : Points and Pixels
- Windows Phone 7 : Changing Caller ID Settings
- Windows Phone 7 : Forwarding Calls
- Windows Phone 7 : Checking Voicemail
- Windows Phone 7 : Making Conference Calls
- Programming Windows Phone 7 : Color Themes
- Programming Windows Phone 7 : The Standard Silverlight Files
- Programming Windows Phone 7 : A First Silverlight Phone Program
- Programming Windows Phone 7 : Sensors and Services
- Programming Windows Phone 7 : The Hardware Chassis
- Windows Phone 7 : Deleting Music or Video
- Windows Phone 7 : Pinning Favorites to Start
- Windows Phone 7 : Listening to FM Radio
- Windows Phone 7 : Playing Podcasts
- Windows Phone 7 : Watching Videos
- Windows Phone 7 : Controlling Music Playback
- Windows Phone 7 : Playing Music
- Windows Phone 7 : Pinning a Favorite Place to Start
- Windows Phone 7 : Adding a Pushpin
- Windows Phone 7 : Changing Map Views
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us